# -*- coding: utf-8 -*-
import sys
import logging
from lxml import etree
from xrisks.model import *
from xrisks.lib.base import *

log = logging.getLogger(__name__)

class RiskindicatorsController(BaseController):
    """REST Controller styled on the Atom Publishing Protocol"""
    # To properly map this controller, ensure your config/routing.py file has
    # a resource setup:
    #     map.resource('riskindicator', 'riskindicators')


    def index(self, format='html'):
        """GET /riskindicators: All items in the collection."""
        # url_for('riskindicators')
        risks = Session.query(Risk).all()
        c.total = len(risks)
        c.total_exp_alta = 0
        c.total_exp_significativa = 0
        c.total_exp_moderada = 0
        c.total_exp_inferior = 0
        c.total_exp_baja = 0
        for risk in risks:
            if risk.probability is not None and risk.impact is not None:
                exposicion = risk.probability * risk.impact
            else:
                exposicion = 0
            if exposicion > 6.4 and exposicion <= 10.0:
                c.total_exp_alta = c.total_exp_alta + 1
            elif exposicion > 3.6 and exposicion <= 6.4:
                c.total_exp_significativa = c.total_exp_significativa + 1
            elif exposicion > 1.6 and exposicion <= 3.6:
                c.total_exp_moderada = c.total_exp_moderada + 1
            elif exposicion > 0.4 and exposicion <= 1.6:
                c.total_exp_inferior = c.total_exp_inferior + 1
            elif exposicion >= 0 and exposicion <= 0.4:
                c.total_exp_baja = c.total_exp_baja + 1

        response.headers['Content-Type'] = 'text/xml; charset=utf-8'
        return render('/riskindicators/indicators.mako')

    def create(self):
        """POST /riskindicators: Create a new item."""
        # url_for('riskindicators')
        pass

    def new(self, format='html'):
        """GET /riskindicators/new: Form to create a new item."""
        # url_for('new_riskindicator')
        pass

    def update(self, id):
        """PUT /riskindicators/id: Update an existing item."""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="PUT" />
        # Or using helpers:
        #    h.form(h.url_for('riskindicator', id=ID),
        #           method='put')
        # url_for('riskindicator', id=ID)
        pass

    def delete(self, id):
        """DELETE /riskindicators/id: Delete an existing item."""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="DELETE" />
        # Or using helpers:
        #    h.form(h.url_for('riskindicator', id=ID),
        #           method='delete')
        # url_for('riskindicator', id=ID)
        pass

    def show(self, id, format='html'):
        """GET /riskindicators/id: Show a specific item."""
        # url_for('riskindicator', id=ID)
        pass

    def edit(self, id, format='html'):
        """GET /riskindicators/id;edit: Form to edit an existing item."""
        # url_for('edit_riskindicator', id=ID)
        pass
